<?php

namespace Admin\Controller;

class CodeController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Code");
    }

    //首页
    public function index() {

//        $requestMap = $this->convertRequestToMap();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["c.enterprise_code"] = $user["enterprise_code"];

        $requestMap["c.level"] = 0;
        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $requestMap0["c.level"] = 0;
        if(!isNullOrEmpty($_REQUEST['level0'])){
            $requestMap0["c.name-like"] = $_REQUEST['level0'];
            $this->assign('level0', $_REQUEST['level0']);
        }
        $requestMap0["c.enterprise_code"] = $user["enterprise_code"];
        $list0 = $this->currentModel->queryForList($page, $requestMap0);

        $requestMap1["c.level"] = 1;
        if(!isNullOrEmpty($_REQUEST['level1'])){
            $requestMap1["c.name-like"] = $_REQUEST['level1'];
            $this->assign('level1', $_REQUEST['level1']);
        }
        $requestMap1["c.enterprise_code"] = $user["enterprise_code"];
        $list1 = $this->currentModel->queryForList($page, $requestMap1);

        $this->assign('list0', $list0);
        $this->assign('list1', $list1);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }
    public function remove() {

        $id = $_REQUEST['id'];
        $data = $this->currentModel->queryById($id);
        if($data["level"] == 0){
            $codeList = D('Code')->where(array('parentid'=>$id))->select();
            foreach ($codeList as $code) {
                $this->currentModel->deleteById($code["code_id"]);
            }
        }

        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("Code/index"));

    }

    public function add() {
        $codeId = $_REQUEST["id"];
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $levelCode = D('Code')->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->where(array('c.enterprise_code'=>$user["enterprise_code"]))
            ->field("c.*, c.name as code_name, p.name as parent_name")
            ->select();

        $this->assign("user", $user);
        $this->assign("levelCode", $levelCode);
        $this->assign("codeId", $codeId);
        $this->display("add");
    }

    /**
     * 添加数据
     */
    public function addPost() {
        $data["parentid"] = $_REQUEST["parentId"];
        $data["name"] = $_REQUEST["name"];
        if($_REQUEST["parentId"] == 0){
            $data["level"] = 0;
            $data["full_name"] = $data["name"];
        }else{
            $data["level"] = 1;
            $data["full_name"] = $_REQUEST["parentName"] . "." .$data["name"];
        }

        $data["remark"] = $_REQUEST["remark"];
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["update_id"] = $user["employee_id"];
        $data["enterprise_code"] = $user["enterprise_code"];
        $this->currentModel->add($data);

        $this->success("添加成功", U("Code/index"));

    }

    public function edit() {
        $id = $_REQUEST["id"];
        $info = D("Code")->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->where(array('c.code_id'=>$id))
            ->field("c.*, p.name as parent_name")
            ->find();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $levelCode = D('Code')->alias("c")
            ->join("left join __CODE__ p on p.code_id = c.parentid")
            ->where(array('c.enterprise_code'=>$user["enterprise_code"]))
            ->field("c.*, c.name as code_name, p.name as parent_name")
            ->select();

        $this->assign("user", $user);
        $this->assign("levelCode", $levelCode);
        $this->assign("info", $info);
        $this->display("edit");
    }
    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];
        $data = $this->currentModel->queryById($id);
        $data["parentid"] = $_REQUEST["parentId"];
        $data["name"] = $_REQUEST["name"];
        if($_REQUEST["parentId"] == 0){
            $data["level"] = 0;
            $data["full_name"] = $data["name"];
        }else{
            $data["level"] = 1;
            $data["full_name"] = $_REQUEST["parentName"] . "." .$data["name"];
        }

        $data["remark"] = $_REQUEST["remark"];
        $data["update_time"] = getCurrentDate();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["update_id"] = $user["employee_id"];

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("Code/index"));

    }
}